﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Young3.FMSearch.Business.Entities.InGame;
using Young3.FMSearch.Business.Managers;

namespace Young3.FMSearch.Interface
{

    /// <summary>
    /// Class containing all information about the
    /// FM game
    /// </summary>
    public class FMContext : IDisposable
    {
        /// <summary>
        /// Initializes the game world!
        /// </summary>
        /// 
        ObjectManager objectManager = null;
        GameManager gameManager = null;

        public FMContext()
        {
            
        }

        public List<Country> Countries { get; set; }
        public List<Player> Players { get; set; }
        public List<Club> Clubs { get; set; }
        public List<Team> Teams { get; set; }
        public List<Continent> Continents { get; set; }
        public List<City> Cities { get; set; }
        public List<Stadium> Stadiums { get; set; }
        public List<Staff> NonPlayingStaff { get; set; }
        public List<HumanManager> HumanManagers { get; set; }
        public List<League> Leagues { get; set; }
        public List<Weather> Weather { get; set; }

        public DateTime IngameDate
        {
            get
            {
                return Global.IngameDate;
            }
        }

        public bool LoadGame()
        {
            if (gameManager == null)
                gameManager = new GameManager();

            return gameManager.CheckProcessAndGame();
        }

        public bool FMLoaded()
        {
            return gameManager.FMLoaded();
        }

        public string GetCurrentFMVersion()
        {
            return gameManager.GetCurrentFMVersion();
        }

        public void LoadData()
        {
            if (objectManager == null)
                objectManager = new ObjectManager();

            objectManager.Load();
            Countries = objectManager.Countries;
            Players = objectManager.Players;
            Clubs = objectManager.Clubs;
            Teams = objectManager.Teams;
            Continents = objectManager.Continents;
            Cities = objectManager.Cities;
            Stadiums = objectManager.Stadiums;
            NonPlayingStaff = objectManager.NonPlayingStaff;
            HumanManagers = objectManager.HumanManagers;
            Leagues = objectManager.Leagues;
            Weather = objectManager.Weather;
        }

        public Player ActivePlayerObject
        {
            get
            {
                int objAddress = Global.getObjectAddress;

                if (objectManager.Players.Where(a => a.MemoryAddress == objAddress - 236).Count() > 0)
                    return objectManager.Players.Single(a => a.MemoryAddress == objAddress - 236);
                else
                    return null;
            }
        }

        public Staff ActiveStaffObject
        {
            get
            {
                int objAddress = Global.getObjectAddress;

                if (objectManager.NonPlayingStaff.Where(a => a.MemoryAddress == objAddress - 56).Count() > 0)
                    return objectManager.NonPlayingStaff.Single(a => a.MemoryAddress == objAddress - 56);
                else
                    return null;
            }
        }

        public Team ActiveTeamObject
        {
            get
            {
                int objAddress = Global.getObjectAddress;

                if (objectManager.Teams.Where(a => a.MemoryAddress == objAddress).Count() > 0)
                    return objectManager.Teams.Single(a => a.MemoryAddress == objAddress);
                else
                    return null;
            }
        }

        public HumanManager ActiveHumanManager
        {
            get
            {
                int objAddress = Global.getObjectAddress;

                if (objectManager.HumanManagers.Where(a => a.MemoryAddress == objAddress - 612).Count() > 0)
                    return objectManager.HumanManagers.Single(a => a.MemoryAddress == objAddress - 612);
                else
                    return null;
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            objectManager = null;
            Countries = null;
            Players = null;
            Clubs = null;
            Teams = null;
            Continents = null;
            Cities = null;
            Stadiums = null;
            NonPlayingStaff = null;
        }

        #endregion
    }
}
